<#include "../../inc/layout.html"><@layout>
<script type="text/javascript" charset="utf-8">
	var editRow;
	var editType;/*add or edit or undefined*/
	var treegrid;
	$(function() {
		treegrid = $('#treegrid').treegrid({
			url:'parame/treeDataGrid',
			toolbar:[ {
				text:'展开',
				iconCls:'icon-redo',
				handler:function() {
					var node = treegrid.treegrid('getSelected');
					if (node) {
						treegrid.treegrid('expandAll',node.ID);
					} else {
						treegrid.treegrid('expandAll');
					}
				}
			},'-',{
				text:'折叠',
				iconCls:'icon-undo',
				handler:function() {
					var node = treegrid.treegrid('getSelected');
					if (node) {
						treegrid.treegrid('collapseAll',node.ID);
					} else {
						treegrid.treegrid('collapseAll');
					}
				}
			},'-',{
				text:'刷新',
				iconCls:'icon-reload',
				handler:function() {
					editRow = undefined;
					editType = undefined;
					treegrid.treegrid('reload');
				}
			},'-',{
				text:'增加',
				iconCls:'icon-add',
				handler:function() {
					append();
				}
			},'-',{
				text:'删除',
				iconCls:'icon-remove',
				handler:function() {
					remove();
				}
			},'-',{
				text:'编辑',
				iconCls:'icon-edit',
				handler:function() {
					edit();
				}
			},'-',{
				text:'保存',
				iconCls:'icon-save',
				handler:function() {
					if (editRow != undefined) {
						treegrid.treegrid('endEdit',editRow.ID);
					}
				}
			},'-',{
				text:'取消编辑',
				iconCls:'icon-undo',
				handler:function() {
					if (editRow) {
						treegrid.treegrid('cancelEdit',editRow.ID);
						var p = treegrid.treegrid('getParent',editRow.ID);
						if (p) {
							treegrid.treegrid('reload',p.ID);
						} else {
							treegrid.treegrid('reload');
						}
						editRow = undefined;
						editType = undefined;
					}
				}
			},'-',{
				text:'取消选中',
				iconCls:'icon-undo',
				handler:function() {
					treegrid.treegrid('unselectAll');
				}
			},'-' ],
			title:'',
			iconCls:'icon-save',
			fit:true,
			fitColumns:false,
			nowrap:false,
			animate:false,
			border:false,
			idField:'ID',
			treeField:'CNAME',
			frozenColumns:[ [ {
				title:'ID',
				field:'ID',
				width:150,
				hidden:true
			},{
				field:'CNAME',
				title:'参数名称',
				width:180,
				editor:{
					type:'validatebox',
					options:{
						required:true
					}
				}
			} ] ],
			columns:[ [
			{
				field:'VAL',
				title:'数值',
				editor:{
					type:'validatebox',
					options:{
					}
				}
			},{
				field:'TYPE',
				title:'参数类型',
				editor:{
					type:'validatebox',
					options:{
					}
				}
			},{
				field:'SORTNUM',
				title:'排序号',
				editor:{
					type:'validatebox',
					options:{
					}
				}
			},{
				field:'REMARK',
				title:'备注',
				editor:{
					type:'text'
				}
			},{field:'PID',
				title:'上级参数项',
				width:160,
				formatter : function(value, rowData, rowIndex) {
					return rowData.PCNAME;
				},
				editor : {
					type : 'combotree',
					options : {
						url : 'parame/combotree',
						animate : false,
						lines : !sy.isLessThanIe8(),
						onLoadSuccess : function(row, data) {
							var t = $(this);
							if (data) {
								$(data).each(function(index, d) {
									if (this.state == 'closed') {
										t.tree('expandAll');
									}
								});
							}
						}
					}
				}
				
			}] ],
			onDblClickRow:function(row) {
				if (editRow != undefined) {
					treegrid.treegrid('endEdit',editRow.ID);
				}

				if (editRow == undefined) {
					treegrid.treegrid('beginEdit',row.ID);
					editRow = row;
					editType = 'edit';
					treegrid.treegrid('unselectAll');
				}
			},
			onAfterEdit:function(row,changes) {
				if (editType == undefined) {
					editRow = undefined;
					treegrid.treegrid('unselectAll');
					return;
				}

				var url = '';
				if (editType == 'add') {
					url = 'parame/save';
				}
				if (editType == 'edit') {
					url = 'parame/save';
				}

				$.ajax({
					url:url,
					data:row,
					dataType:'json',
					success:function(r) {
						if (r.success) {
							treegrid.treegrid('reload');
							$.messager.show({
								msg:r.msg,
								title:'成功'
							});
							editRow = undefined;
							editType = undefined;
						} else {
							treegrid.treegrid('beginEdit',editRow.ID);
							$.messager.alert('错误',r.msg,'error');
						}
					}
				});
			},
			onContextMenu:function(e,row) {
				e.preventDefault();
				$(this).treegrid('unselectAll');
				$(this).treegrid('select',row.ID);
				$('#menu').menu('show',{
					left:e.pageX,
					top:e.pageY
				});
			},
			onLoadSuccess:function(row,data) {
				/*var t = $(this);
				if (data) {
					$(data).each(function(index,d) {
						if (this.state == 'closed') {
							t.treegrid('expandAll');
						}
					});
				}*/
			},
			onExpand:function(row) {
				treegrid.treegrid('unselectAll');
			},
			onCollapse:function(row) {
				treegrid.treegrid('unselectAll');
			}
		});

	});

	function edit() {
		var node = treegrid.treegrid('getSelected');
		if (node && node.ID) {
			if (editRow != undefined) {
				treegrid.treegrid('endEdit',editRow.ID);
			}
			if (editRow == undefined) {
				treegrid.treegrid('beginEdit',node.ID);
				editRow = node;
				editType = 'edit';
			}
		} else {
			$.messager.show({
				msg:'请选择一项进行修改！',
				title:'错误'
			});
		}
	}
	function append() {
		if (editRow != undefined) {
			treegrid.treegrid('endEdit',editRow.ID);
		}

		if (editRow == undefined) {
			var node = treegrid.treegrid('getSelected');
			var row = [ {
				CNAME:'',
				URL:'',
				SORTNUM:1,
				ICON: 'icon-calc',
				pid:node == null ? 0:node.ID
			} ];
			treegrid.treegrid('append',{
				parent:node == null ? '':node.ID,
				data:row
			});

			editRow = row[0];
			editType = 'add';
			treegrid.treegrid('select',editRow.ID);
			treegrid.treegrid('beginEdit',editRow.ID);
		}
	}
	function remove() {
		var node = treegrid.treegrid('getSelected');
		if (node) {
			$.messager.confirm('询问','您确定要删除【' + node.CNAME + '】？',function(b) {
				if (b) {
					$.ajax({
						url:'parame/del/'+node.ID,
						data:{
						},
						cache:false,
						dataType:"json",
						success:function(r) {
							if (r.success) {
								treegrid.treegrid('remove',r.obj);
								$.messager.show({
									msg:r.msg,
									title:'提示'
								});
								editRow = undefined;
							} else {
								$.messager.alert("操作错误",r.msg,'error');
							}
						}
					});
				}
			});
		}
	}
</script>
</head>
<body class="easyui-layout" fit="true">
	<div region="center" border="false" style="overflow: hidden;">
		<table id="treegrid"></table>
	</div>

	<div id="menu" class="easyui-menu" style="width:120px;display: none;">
		<div onclick="append();" iconCls="icon-add">增加</div>
		<div onclick="remove();" iconCls="icon-remove">删除</div>
		<div onclick="edit();" iconCls="icon-edit">编辑</div>
	</div>
</@layout>